(function flexible(window, document) {
    var docEl = document.documentElement;
    var rem = docEl.clientWidth / 37.5;

    // set 1rem = 10px
    function setRemUnit() {
        rem = docEl.clientWidth / 37.5
        docEl.style.fontSize = rem + 'px'
    }

    setRemUnit()

    window.addEventListener('resize', setRemUnit)
    window.addEventListener('pageshow', function (e) {
        if (e.persisted) {
            setRemUnit()
        }
    })

    window.rem2px = function (d) {
        var val = parseFloat(d) * rem;
        if (typeof d === 'string' && d.match(/rem$/)) {
            val += 'px';
        }
        return val;
    }
    window.px2rem = function (d) {
        var val = parseFloat(d) / rem;
        if (typeof d === 'string' && d.match(/px$/)) {
            val += 'rem';
        }
        return val;
    }
}(window, document))
